<template>
  <Modal v-model="show" width="1000" title="新增用户">
    <UserForm
      ref="userForm_ref"
      :form-data="formData"
      :hide-list="{ pwd: true }"
      @on-user-submit="handleUserSubmit"
    ></UserForm>

    <template #footer>
      <div class="text-center mt20 mb20">
        <Button class="ivu-btn--round" @click="close">取消</Button>
        <Button class="ivu-btn--round" type="primary" @click="handleSave">保存</Button>
      </div>
    </template>
  </Modal>
</template>

<script>
import UserForm from './UserForm.vue'

// util
import { handleRequestStatus } from '@/utils/uitls.js'
import { createForm } from './service'

// request
import { systemUserInsert } from '@/api/system/userConfig.js'

export default {
  name: 'ModalCreateUser',

  props: {
    visible: Boolean,
  },

  components: { UserForm },

  data() {
    return {
      formData: createForm(),
    }
  },

  computed: {
    show: {
      get() {
        return this.visible
      },
      set(v) {
        this.$emit('update:visible', v)
      },
    },
  },

  methods: {
    close() {
      this.show = false
      setTimeout(() => (this.formData = createForm()), 50)
    },

    handleSave() {
      this.$refs['userForm_ref'].handleFormSubmit()
    },

    handleUserSubmit(data) {
      // console.log('handleUserSubmit', data)
      this.systemUserInsert(data)
    },

    async systemUserInsert(params) {
      const res = await systemUserInsert(params)
      handleRequestStatus(res, '新增', () => {
        this.$emit('on-add-success')
        this.close()
      })
    },
  },
}
</script>

<style lang="less" scoped></style>
